package group.ocr.Controller;

import group.ocr.Config.ImageConfig;
import group.ocr.Config.SingleCharactersConfig;
import group.ocr.Service.ImageService;
import group.ocr.Service.OCRService;
import group.ocr.Service.SingleCharactersService;
import group.ocr.pojo.image;
import group.ocr.pojo.singleCharacters;
import org.codehaus.jettison.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.List;

@RestController
public class ImageController {
    @Autowired
    private OCRService ocrService;
    @Autowired
    private ImageService imageService;
    @Autowired
    private SingleCharactersService singleCharactersService;
    @Autowired
    private ImageConfig imageConfig;
    @Autowired
    private SingleCharactersConfig singleCharactersConfig;
    @PostMapping("OCR")
    public String OCR(@RequestParam("image") MultipartFile image) throws JSONException, IOException {
        return ocrService.imageRecognition(image);
    }
    @GetMapping("search")
    public List<singleCharacters> search(
            @RequestParam(required = false) String characterName,
            @RequestParam(required = false) String font,
            @RequestParam(required = false) String author,
            @RequestParam(required = false) String title) {
        return singleCharactersService.search(characterName, font, author, title);
    }
    @PostMapping("saveImage")
    public String saveImage(@RequestParam("image") MultipartFile image) throws JSONException, IOException {
        return imageService.saveImage(image);
    }
    @PostMapping("saveSingleCharacters")
    public String saveSingleCharacters(@RequestParam("image") MultipartFile image,String title,String font ,String author) throws JSONException, IOException {
        return singleCharactersService.saveSingleCharacters(image,title,font,author);

    }
    @GetMapping("collectImage")
    public String collectImage(int id){
        imageService.collectImage(id);
        return "已收藏作品id：" + id;
    }
    @GetMapping("collection")
    public List<image>collection(){
        return imageConfig.getImageCollection();
    }
    @GetMapping("collectSingleCharacter")
    public String collectSingleCharacter(int id){
        singleCharactersService.collectSingleCharacter(id);
        return "已收藏单字id：" + id;
    }
    @GetMapping("singleCollection")
    public List<singleCharacters>singleCollection(){
        return singleCharactersConfig.getSingleCharactersCollection();
    }
    @GetMapping("selectAllImages")
    public List<image>selectAllImages(){
        return imageService.selectAllImages();
    }
    @GetMapping("selectAllSingleCharacters")
    public List<singleCharacters>selectAllSingleCharacters(){
        return singleCharactersService.selectAllSingleCharacters();
    }
}
